//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.action.BaseFileAction;
import net.mingsoft.basic.bean.UploadConfigBean;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.config.MSProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"前端-基础接口"}
)
@Controller
@RequestMapping({"/file"})
public class FileAction extends BaseFileAction {
    public FileAction() {
    }

    @ApiOperation("处理post请求上传文件")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "uploadPath",
            value = "上传文件夹地址",
            required = false,
            paramType = "form"
    ), @ApiImplicitParam(
            name = "file",
            value = "文件流",
            dataType = "__file",
            required = false,
            paramType = "form"
    ), @ApiImplicitParam(
            name = "rename",
            value = "是否重命名",
            required = false,
            paramType = "form",
            defaultValue = "true"
    ), @ApiImplicitParam(
            name = "appId",
            value = "上传路径是否需要拼接appId",
            required = false,
            paramType = "form",
            defaultValue = "false"
    )})
    @PostMapping({"/upload"})
    @ResponseBody
    public ResultData upload(@ApiIgnore UploadConfigBean bean, HttpServletRequest req, HttpServletResponse res) throws IOException {
        boolean uploadEnable = MSProperties.upload.enableWeb;
        if (!uploadEnable) {
            return ResultData.build().error(this.getResString("insufficient.permissions"));
        } else if (bean.getUploadPath() == null || !bean.getUploadPath().contains("../") && !bean.getUploadPath().contains("..\\")) {
            if (bean.isAppId()) {
                bean.setUploadPath(BasicUtil.getApp().getAppId() + File.separator + bean.getUploadPath());
            }

            UploadConfigBean config = new UploadConfigBean(bean.getUploadPath(), bean.getFile(), (String)null, false, bean.isRename());
            return this.upload(config);
        } else {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("file.path")}));
        }
    }
}
